<?php  
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
class User extends Controller
{
	// 用户注册
    public function register()
    {
        return view('register');
    }
    // 用户注册操作
    public function register_add()
    {
        $time = time();
        $usersql = "select * from man_user";
        $userinfo = Db::query($usersql);
        $data = input('post.');
        if(!captcha_check($data['code'])) {
            // 校验失败
            $this->error('验证码不正确');
        }else{
            if ($userinfo[0]['u_name']==$data['u_name']){
                $this->error('该用户名已存在');
            }else{
                $sql = "insert into man_user values ('','','".$data['u_name']."','".md5($data['u_pwd'])."','','$time','','','')";
                $res = Db::execute($sql);
                if ($res){
                    $this->success('注册成功');
                }
            }
        }
    }
    
    // 登录
    public function login()
    {
        return view('login');
    }
    // 登录操作
    public function loginOn()
    {
        $data = input('post.');
        $pwd = md5($data['u_pwd']);
        $usersql = "select * from man_user where u_name='$data[u_name]' and u_pwd='$pwd'";
        $userinfo = Db::query($usersql);
        if(!captcha_check($data['code'])) {
            // 校验失败
            $this->error('验证码不正确');
        }else{
            if ($pwd==$userinfo[0]['u_pwd']){
                Cookie::set('name',$userinfo[0]['u_name'],3600);//两小时后过期
                Cookie::set('u_id',$userinfo[0]['u_id'],3600);
                $this->success('登录成功','index/index');
            }else{
                $this->error('密码错误');
            }
        }
    }
    // 登出
    public function loginOut()
    {
        Cookie::set('name',null);
        Cookie::set('u_id',null);
        return $this->redirect('user/login');
    }

    // 我的个人中心
    public function userindex()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            // 自动删除过期的订单
            $this->orderGqDd($u_id);

            $name = Cookie::get('name');
            // 查询收藏表
            $sql = "select * from man_product_sc where u_id=$u_id and sc_statc=1";
            $scData = Db::query($sql);

            $goodsData = array();
            foreach ($scData as $key => $value) {
                // 查询收藏的商品
                $sql = "select * from man_goods where g_id='$value[g_id]'";
                $goodData = Db::query($sql);
                $goodsData[] = $goodData[0];
            }
            // 查询未付款订单数量
            $sql = "select count(o_id) as num from man_order where u_id=$u_id and u_del=0 and o_back=0 and o_statc=1";
            $payment = Db::query($sql);
            // 查询未发货订单数量
            $sql = "select count(o_id) as num from man_order where u_id=$u_id and u_del=0 and o_back=0 and o_statc=2";
            $dropship = Db::query($sql);
            // 查询未收货订单数量
            $sql = "select count(o_id) as num from man_order where u_id=$u_id and u_del=0 and o_back=0 and o_statc=3";
            $wait = Db::query($sql);
            // 查询未评价订单数量
            $sql = "select count(o_id) as num from man_order where u_id=$u_id and u_del=0 and o_back=0 and o_statc=4 and o_remark=''";
            $main = Db::query($sql);

            $typeData = $this->alltype();
            return view('userindex',[
                'typeData'=>$typeData,
                'scData'=>$scData,
                'goodsData'=>$goodsData,
                'name'=>$name,
                'payment'=>$payment,
                'dropship'=>$dropship,
                'wait'=>$wait,
                'main'=>$main
                ]
            );
        }else{
            return redirect("user/login");
        }
    }
    // 自动删除过期的订单
    public function orderGqDd($u_id)
    {
        $time = time();
        $sql = "select o_id from man_order where u_id=$u_id and o_addtime+86400<$time and o_statc=1 and u_del=0";
        $orderData = Db::query($sql);
        foreach ($orderData as $key => $value) {
            $sql = "update man_order set u_del=1 where o_id='$value[o_id]' and u_id=$u_id and o_statc=1";
            Db::execute($sql);
        }
    }

    // 我的个人资料
    public function usersub()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $u_name = COOKIE('name');
            $sql = "select * from man_user where u_name = '$u_name' and u_id='$u_id'";
            $data = Db::query($sql);
            $typeData = $this->alltype();
            return view('usersub',['typeData'=>$typeData,'data'=>$data[0]]);
        }else{
            return redirect("user/login");
        }
    }
    //完善资料
    public function userinfo(){
        $data = input('post.');
        $u_id = Cookie::get('u_id');
        $u_photo = request()->file('u_photo');
        if(!empty($u_photo)){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $u_photo->move(ROOT_PATH . 'public' . DS . 'upload');
            if ($info){
                $path = $info->getFilename();
                $path = "upload/".date('Ymd',time())."/".$path;
            }
            $sql = "update man_user set u_nickname='".$data['u_nickname']."',u_photo='$path',u_tel='".$data['u_tel']."',u_email='".$data['u_email']."',u_sex='".$data['u_sex']."' where u_id='$u_id'";
        }else{
            $sql = "update man_user set u_nickname='".$data['u_nickname']."',u_tel='".$data['u_tel']."',u_email='".$data['u_email']."',u_sex='".$data['u_sex']."' where u_id='$u_id'";
        }
        $res = Db::execute($sql);
        if ($res){
            $this->redirect('user/usersub');
        }
    }
    //修改密码
    public function uppwd(){
        return view('uppwd');
    }
    //修改密码
    public function uppwddo(){
        $data = input();
        $u_id = Cookie::get('u_id');
        $sql = "select * from man_user where u_id='$u_id'";
        $userinfo = Db::query($sql);
        if ($data['u_pwd']==$userinfo[0]['u_pwd']){
            $sql = "update man_user set u_pwd='".$data['npwd']."' where u_id='$u_id'";
            $res = Db::execute($sql);
            if ($res){
                $this->success('修改成功');
            }
        }else{
            $this->error('原密码错误');
        }
    }
    
    // 我的收藏
    public function collect()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $sql = "select * from man_product_sc where u_id=$u_id and sc_statc=1";
            $scData = Db::query($sql);

            $goodsData = array();
            foreach ($scData as $key => $value) {
                // 查询收藏的商品
                $sql = "select * from man_goods where g_id='$value[g_id]'";
                $goodData = Db::query($sql);
                $goodsData[] = $goodData[0];
            }
            $typeData = $this->alltype();
            return view('collect',['typeData'=>$typeData,'scData'=>$scData,'goodsData'=>$goodsData]);
        }else{
            return redirect("user/login");
        }
        
    }

    // 申请退货
    public function usertui()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){

            $request = Request::instance();
            // 当前页
            $page = $request->get('page',1);
            // 每页显示条数
            $pagesize = 5;
            // 偏移量
            $offest = ($page-1)*$pagesize;
            $sql = "select count(*) as num from man_order where u_id=$u_id and u_del=0";
            $res = Db::query($sql);
            // 尾页
            $lastpage = ceil($res[0]['num']/$pagesize);
            // 上一页
            $uppage = $page -  1 < 1 ? 1 :$page-1;
            // 下一页
            $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
            $page = array(
                'page'=>$page,
                'uppage'=>$uppage,
                'downpage'=>$downpage,
                'lastpage'=>$lastpage
            );
            // 查询退货订单
            $sql = "select * from man_order where u_id=$u_id and u_del=0 and o_back in(1,2,3) and o_statc in(8,9,10) order by o_addtime desc limit $offest,$pagesize";
            $orderData = Db::query($sql);

            $typeData = $this->alltype();
            return view('usertui',['typeData'=>$typeData,"orderData"=>$orderData]);
        }else{
            return redirect("user/login");
        }
    }
    // 申请退货记录
    public function usertuijl()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $request = Request::instance();
            // 当前页
            $page = $request->get('page',1);
            // 每页显示条数
            $pagesize = 5;
            // 偏移量
            $offest = ($page-1)*$pagesize;
            $sql = "select count(*) as num from man_order where u_id=$u_id and u_del=0";
            $res = Db::query($sql);
            // 尾页
            $lastpage = ceil($res[0]['num']/$pagesize);
            // 上一页
            $uppage = $page -  1 < 1 ? 1 :$page-1;
            // 下一页
            $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
            $page = array(
                'page'=>$page,
                'uppage'=>$uppage,
                'downpage'=>$downpage,
                'lastpage'=>$lastpage
            );
            // 查询退货订单
            $sql = "select * from man_order where u_id=$u_id and u_del=0 and o_back=4 and o_statc=11 order by o_addtime desc limit $offest,$pagesize";
            $orderData = Db::query($sql);

            $typeData = $this->alltype();
            return view('usertuijl',['typeData'=>$typeData,"orderData"=>$orderData]);
        }else{
            return redirect("user/login");
        }
    }

    // 收货地址
    public function newPath()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            // 收货地址
            $sql = "select * from man_address where u_id=$u_id";
            $addressData = Db::query($sql);

            // 省市区联动
            if($this->request->isAjax()){
                $area_id = input("get.area_id");
                $sql = "select * from man_area where area_parent_id=$area_id";
                $areaData = Db::query($sql);
                $return = array('areaData'=>$areaData);
                echo json_encode($return);
                die;
            }
            $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
            $areaData = Db::query($sql);

            $typeData = $this->alltype();
            return view('newPath',['typeData'=>$typeData,'areaData'=>$areaData,'addressData'=>$addressData]);
        }else{
            return redirect("user/login");
        }
        
    }
    // 设置默认
    public function newPathMr()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $ad_id = input("get.ad_id");
            $sql = "update man_address set ad_is_default=1 where u_id=$u_id and ad_id=$ad_id";
            Db::execute($sql);
            $sql = "update man_address set ad_is_default=0 where u_id=$u_id and ad_id!=$ad_id";
            Db::execute($sql);
            echo "1";
        }else{
            return redirect("user/login");
        }
    }
    // 收货地址添加
    public function newPathAdd()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $data = input("post.");
            if(!empty($data)){
                $sql = "select count(*) as num from man_address where u_id=$u_id";
                $area = Db::query($sql);
                if($area['0']['num']<5){
                    $str = '';
                    foreach ($data['country'] as $key => $value) {
                        $str .= ",".$value;
                    }
                    // 省市区id
                    $ad_address_id = substr($str,1);

                    $sql = "select area_name from man_area where area_id in($ad_address_id)";
                    $areaName = Db::query($sql);

                    $str = '';
                    foreach ($areaName as $key => $value) {
                        $str .= ",".$value['area_name'];
                    }
                    // 省市区+详情地址
                    $ad_address_xq = substr($str,1).','.$data['ad_address'];

                    $sql = "select * from man_address where u_id=$u_id and ad_name='$data[ad_name]' and ad_tel='$data[ad_tel]' and ad_address_id='$ad_address_id' and ad_address='$data[ad_address]' and ad_address_xq='$ad_address_xq' and ad_code='$data[ad_code]'";
                    $area = Db::query($sql);
                    if(!$area){
                        $sql = "insert into man_address values(null,$u_id,'$data[ad_name]','$data[ad_tel]','$ad_address_id','$data[ad_address]','$ad_address_xq','$data[ad_code]',0)";
                        $areaData = Db::query($sql);
                        echo "0";die;
                    }else{
                        echo "4";die;
                    }
                }else{
                    echo "3";die;
                }
            }else{
                echo "2";die;
            }
        }else{
            echo "1";die;
        }
    }

    // 收货地址修改
    public function newPathupd()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $ad_id = input("get.ad_id");
            // 收货地址
            $sql = "select * from man_address where u_id=$u_id and ad_id=$ad_id";
            $addressData = Db::query($sql);
            $addressInfo = $addressData[0];
            // 省市区联动
            if($this->request->isAjax()){
                $area_id = input("get.area_id");
                $sql = "select * from man_area where area_parent_id=$area_id";
                $areaData = Db::query($sql);
                $return = array('areaData'=>$areaData);
                echo json_encode($return);
                die;
            }
            $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
            $areaData = Db::query($sql);

            $typeData = $this->alltype();
            return view('newPathupd',['typeData'=>$typeData,'areaData'=>$areaData,'addressInfo'=>$addressInfo]);
        }else{
            return redirect("user/login");
        }
        
    }
    // 收货地址修改
    // public function newPathUpdOne()
    // {
    //     $u_id = Cookie::get('u_id');
    //     if(!empty($u_id)){
    //         $data = input("post.");
    //         if(!empty($data)){
    //                 $str = '';
    //                 foreach ($data['country'] as $key => $value) {
    //                     $str .= ",".$value;
    //                 }
    //                 // 省市区id
    //                 $ad_address_id = substr($str,1);

    //                 $sql = "select * from man_area where area_id in($ad_address_id)";
    //                 $areaName = Db::query($sql);

    //                 $str = '';
    //                 foreach ($areaName as $key => $value) {
    //                     $str .= ",".$value['area_name'];
    //                 }
    //                 // 省市区+详情地址
    //                 $ad_address_xq = substr($str,1).','.$data['ad_address'];

    //                 $sql = "select * from man_address where u_id=$u_id and ad_name='$data[ad_name]' and ad_tel='$data[ad_tel]' and ad_address_id='$ad_address_id' and ad_address='$data[ad_address]' and ad_address_xq='$ad_address_xq' and ad_code='$data[ad_code]'";
    //                 $area = Db::query($sql);
    //                 if(!$area){
    //                     $sql = "update man_address set ad_name='$data[ad_name]',ad_tel='$data[ad_tel]',ad_address_id='$ad_address_id',ad_address='$data[ad_address]',ad_address_xq='$ad_address_xq',ad_code='$data[ad_code]' where u_id=$u_id and ad_id='data[ad_id]'";
    //                     $areaData = Db::query($sql);
    //                     echo "0";die;
    //                 }else{
    //                     echo "4";die;
    //                 }
    //         }else{
    //             echo "2";die;
    //         }
    //     }else{
    //         echo "1";die;
    //     }
    // }
    // 收货地址删除
    public function newPathDel()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $ad_id = input("get.ad_id");
            if(!empty($ad_id)){
                $sql = "delete from man_address where ad_id=$ad_id and u_id=$u_id";
                Db::execute($sql);
                echo "0";die;
            }else{
                echo "2";die;
            }
        }else{
            echo "1";die;
        }
    }


    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }

    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }
}


?>